package com.business.action.system;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.business.bean.system.RoleBean;

import net.sf.rose.jdbc.DBUtils;
import net.sf.rose.jdbc.PageBean;
import net.sf.rose.jdbc.dao.BeanDAO;
import net.sf.rose.jdbc.query.BeanSQL;
import net.sf.rose.jdbc.service.Service;
import net.sf.rose.web.util.WebUtils;

/**
 * 角色管理
 */
@Controller
@RequestMapping("/role")
public class RoleAction {

	/**
	 * 
	 */
	@ResponseBody
	@RequestMapping("/page.do")
	public PageBean page(HttpServletRequest request, Service service) {
		PageBean page = WebUtils.getPageBean(request);
		Map<String, Object> map = WebUtils.getRequestData(request);
		/*
		 * if (!handle.isAdmin()) { map.put("instID", handle.getLocalInstID()); }
		 */
		BeanDAO dao = new BeanDAO(service);
		BeanSQL query = dao.getQuerySQL();
		query.setPage(page);
		query.setEntityClass(RoleBean.class);
		query.createSql(map);
		dao.list();
		return page;
	}

	/**
	 * 
	 */
	@ResponseBody
	@RequestMapping("/list.do")
	public PageBean list(HttpServletRequest request, Service service) {
		PageBean page = new PageBean();
		Map<String, Object> map = WebUtils.getRequestData(request);
		BeanDAO dao = new BeanDAO(service);
		BeanSQL query = dao.getQuerySQL();
		query.setEntityClass(RoleBean.class);
		query.createSql(map);
		page.setData(dao.list());
		return page;
	}

	/**
	 * 
	 */
	@ResponseBody
	@RequestMapping("/get.do")
	public PageBean get(HttpServletRequest request, Service service, String roleID) {
		PageBean page = new PageBean();
		RoleBean bean = DBUtils.getBean(service, RoleBean.class, roleID);
		page.setData(bean);
		return page;
	}

	/**
	 * 
	 */
	@ResponseBody
	@RequestMapping("/save.do")
	public PageBean save(HttpServletRequest request, Service service, RoleBean bean) {
		PageBean res = new PageBean();
		// 如果操作用户的所属机构存在，则被他创建的用户也属于同样的机构名下
		/*
		 * String instID = handle.getLocalInstID(); if (instID != null) {
		 * bean.setInstID(instID); }
		 */
		BeanDAO dao = new BeanDAO(service);
		BeanSQL query = dao.getQuerySQL();
		query.setEntityClass(RoleBean.class);
		query.createSql("roleID", bean.getRoleID());
		if (dao.get() != null) {
			query.createUpdateSql(bean);
		} else {
			query.createInsertSql(bean);
		}
		if (dao.update() > 0) {
			res.setMessage("保存信息成功！");
		} else {
			res.setSuccess(false);
			res.setMessage("保存信息失败！");
		}
		return res;
	}

	/**
	 * 删除
	 */
	@ResponseBody
	@RequestMapping("/delete.do")
	public PageBean delete(HttpServletRequest request, Service service, String roleID) {
		return DBUtils.delete(service, RoleBean.class, roleID);
	}
}
